/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package apteka.admin;

import apteka.tables.Product;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;

/**
 *
 * @author Marek
 */

@Component
public class AdminProductValidator {

   public boolean supports(Class<?> klasa) {
    return Product.class.isAssignableFrom(klasa);
  }

   public void validate(Object target, Errors errors) {
        Product product = (Product) target;
        if (product.getCategory().getIdcategory()==0) 
            errors.rejectValue("category.idcategory","WrongCategory.product.category", "Należy wybrać kategorię.");
        if (product.getAmount()<0)
            errors.rejectValue("amount","MinAmount.product.amount","Nieprawidłowa ilość");
        if (product.getPrice()<0)
            errors.rejectValue("price","MinPrice.product.price","Nieprawidłowa cena");
        if (product.getProductname().isEmpty())
            errors.rejectValue("productname","WrongName.product.productname","Nieprawidłowa nazwa");
   }
}
